﻿<Control x:Name="Control" x:Class="DataGridExtensionsSample.FilterWithPopupControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:dgx="clr-namespace:DataGridExtensions;assembly=DataGridExtensions">
  <Control.Template>
    <ControlTemplate>
      <Grid>
        <ToggleButton x:Name="button">
          <ToggleButton.Style>
            <Style TargetType="Control">
              <!-- Only show the button if the filter is active or if the mouse is over the column header -->
              <Style.Triggers>
                <DataTrigger Binding="{Binding Path=Filter, ElementName=Control}" Value="{x:Null}">
                  <Setter Property="Visibility" Value="Hidden"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=IsMouseOver, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGridColumnHeader}}" Value="True">
                  <Setter Property="Visibility" Value="Visible"/>
                </DataTrigger>
              </Style.Triggers>
            </Style>
          </ToggleButton.Style>
          <!-- Reuse the standard icon, but change color to green -->
          <Control Style="{DynamicResource {x:Static dgx:DataGridFilter.IconStyleKey}}" Foreground="Green"/>
        </ToggleButton>
        <Popup x:Name="popup" IsOpen="{Binding Path=IsChecked, ElementName=button, Mode=TwoWay}" 
               AllowsTransparency="True"
               DataContext="{Binding ElementName=Control}" StaysOpen="False">
          <Border Background="White" BorderBrush="Gray" BorderThickness="1" CornerRadius="3" Padding="4">
            <StackPanel>
              <TextBlock Text="Enter the limits:" Margin="0,3"/>
              <Grid>
                <Grid.ColumnDefinitions>
                  <ColumnDefinition Width="Auto"/>
                  <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                  <RowDefinition/>
                  <RowDefinition/>
                </Grid.RowDefinitions>

                <TextBlock Grid.Column="0" Grid.Row="0" Text="Min:"/>
                <TextBox Grid.Column="1" Grid.Row="0" Text="{Binding Path=Minimum}" />
                <TextBlock Grid.Column="0" Grid.Row="1" Text="Max:"/>
                <TextBox Grid.Column="1" Grid.Row="1" Text="{Binding Path=Maximum}" />

              </Grid>
            </StackPanel>
          </Border>
        </Popup>
      </Grid>
    </ControlTemplate>
  </Control.Template>
</Control>
